package com.example.controller;

import com.example.entity.Student;
import com.example.service.StudentService;
import com.example.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") int id){
        Student student = studentService.findById(id);
        if(student != null) return Result.success("200","查询成功！",student);
        else return Result.error("202","未查询到该同学");
    }
    @GetMapping("/findAllByPage")
    public Result findAllByPage(Student student, int pageNum, int pageSize){
        PageInfo<Student> allByPage = studentService.findAllByPage(student, pageNum, pageSize);
        if(allByPage!=null)return Result.success("200","查询成功！",allByPage);
        else return Result.error("202","查询失败");
    }

    @PostMapping("/insert")
    public Result insert(Student student){
        int insert = studentService.insert(student);
        if(insert>0)return Result.success("200","插入成功！",insert);
        else return Result.error("202","插入失败");
    }
    @PutMapping("/update")
    public Result update(Student student){
        int update = studentService.update(student);
        if(update>0)return Result.success("200","更新成功！",update);
        else return Result.error("202","更新失败");
    }
    @DeleteMapping ("/delete")
    public Result delete(int id){
        int delete = studentService.delete(id);
        if(delete>0)return Result.success("200","删除成功！",delete);
        else return Result.error("202","删除失败");
    }
}
